import Promise from '../../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../../utils/regenerator-runtime/runtime'
const utils = require('../../../../utils/util.js');
const app=getApp();
Page({
  data: {
    url:"/api/MemberTask/getTaskDetail",
    changeStatusUrl:"/api/MemberTask/changeStatus",
    list:null,
    task_id:null,
    token:null
  },
   onLoad: async function (params) {
     console.log("onLoad");
     wx.showLoading({
       "title":"加载中..."
     });
    var taskid=params.taskid;
    var status=params.status;   
    this.setData({
      task_id:taskid,
      statusText:status
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._loadData();
    if(result.state){
      var list=result.list;
      this.setData({
        list:list
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
    wx.hideLoading();
  },
  _loadData:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.url,{token:this.data.token,task_id:this.data.task_id},res=>{
          //console.log(res);
          var data=res.data;
          // var data={
          //   state:true,
          //   list:
          //   {
          //     "title":"招商银行",
          //     "status":"已预约",
          //     "type":"1",
          //     "id":"1",
          //     "content":"遇到什么什么情况，将要怎样怎样；",
          //     "info":[
          //       {
          //         "time":"12-06  00:23",
          //         "text":"已预约业务员"
          //       },
          //       {
          //         "time":"12-05  00:23",
          //         "text":"您的预约已提交"
          //       }
          //     ]
          //   }
          // };
          if(data.state){
            //resolve({state:true,list:JSON.parse(data.list)});
            resolve({state:true,list:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  },
  changeStatusTap: async function(e){
    var status=e.currentTarget.dataset.status;
    if(!status) return;
    this.setData({
      status:status
    })
    var result=await this._changeStatusRequset();
    if(result.state){
      var msg=result.msg;
      wx.showModal({
        title:msg,
        showCancel:false,
        complete: data=>{
          // this.onLoad({taskid:this.data.task_id,
          //   status:this.data.statusText});
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
    }
   },
  _changeStatusRequset:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.changeStatusUrl,{token:this.data.token,task_id:this.data.task_id,status:this.data.status},res=>{
          //console.log(res);
          var data=res.data;
          // var data={
          //   state:true,
          //   msg:"提交成功"
          // };
          if(data.state){
            resolve({state:true,msg:data.msg});
            //resolve({state:true,msg:data.msg});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  }
})
